package com.example.design.patterns.冰河.创建型模式.单例;

/**
 * 代码五：懒汉模式-双重锁同步锁单例模式，单例实例第一次使用的时候创建，线程安全
 */
public class SingletonExample5 {
    // 单例对象 volatile + 双重检测机制来禁止指令重排
    private volatile static SingletonExample5 instance = null;

    ;

    private SingletonExample5() {
    }

    private static synchronized SingletonExample5 getInstance() {
        if (instance == null) {
            synchronized (SingletonExample5.class) {
                if (instance == null) {
                    instance = new SingletonExample5();
                }
            }
        }
        return instance;
    }
}
